package com.wanxi.springboot.team.manage.system.service.impl;

import com.wanxi.springboot.team.manage.system.api.CommonResult;
import com.wanxi.springboot.team.manage.system.model.UserLikes;
import com.wanxi.springboot.team.manage.system.mapper.UserLikesMapper;
import com.wanxi.springboot.team.manage.system.service.UserLikesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@Service
public class UserLikesServiceImpl extends ServiceImpl<UserLikesMapper, UserLikes> implements UserLikesService {

    @Autowired(required = false)
    UserLikesMapper userLikesMapper;

    @Override
    public CommonResult updateUserLike(UserLikes userLikes) {
        //判断该用户是否曾经点赞过该单词
        UserLikes userLike = userLikesMapper.getUserLike(userLikes);
        //如果点赞过
        if (userLike!=null) {
            if (userLike.getEnable().equals("yes"))//如果现在是已点赞状态
                userLike.setEnable("no");//则将其变为取消点赞
            else
                userLike.setEnable("yes");//否则点赞
            userLikesMapper.updateUserLike(userLike);
        }else//如果没有点赞过该单词
            userLikesMapper.addUserLike(userLikes);//添加该单词的点赞到用户点赞表
        return CommonResult.success(true);
    }
}
